package com.choupin.choupin.controller;

import cn.hutool.extra.ssh.JschRuntimeException;
import com.choupin.choupin.entity.*;
import com.choupin.choupin.service.ILotteryPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/lottery")
public class LotterypoolController extends BaseController {


    @Autowired
    private ILotteryPool iLotteryPool;

    @CrossOrigin
    @PostMapping("/jumppool")
     public JsonResult<PoolListAndPool> jumpToTheTotteryPool(@RequestBody Map<String,Object> data){
         JsonResult<PoolListAndPool> jsonResult=new JsonResult<>();
        System.out.println("跳转抽奖池！"+(String) data.get("phone"));
        PoolListAndPool list  = iLotteryPool.jumpLotteryPool((String) data.get("topName"), (String) data.get("phone"));
        System.out.println("完成跳转查询！"+list);
        jsonResult.setState(SUCCESS);
        jsonResult.setData(list);

        return jsonResult;
     }

//    将抽奖用户存入消息队列
     @CrossOrigin
     @PostMapping("/addqueue")
     public JsonResult<Void> StoreInQueue(@RequestBody Map<String,Object> data){
         JsonResult<Void>  jsonResult=new JsonResult<>();
        iLotteryPool.addMessage((String) data.get("phone"),(String) data.get("queueMessage"));
        jsonResult.setState(SUCCESS);
        return  jsonResult;
     }

//    中奖计算
     @CrossOrigin
     @PostMapping("/winninglottery")
     public JsonResult<Void> winningTheLottery(@RequestBody Map<String,Object> data){
        JsonResult<Void> jsonResult=new JsonResult<>();
        iLotteryPool.winPrize((String) data.get("queueMessage"),(String)data.get("topName"));
        jsonResult.setState(SUCCESS);
        return  jsonResult;
     }

//     查询是否中奖
      @CrossOrigin
     @PostMapping("/showprize")
     public  JsonResult<Prize>  showPrize(@RequestBody Map<String,Object> data){
        JsonResult<Prize> jsonResult=new JsonResult<>();
        Prize result=iLotteryPool.showPrize((String) data.get("phone"),(String)data.get("topName"));
        jsonResult.setState(SUCCESS);
        jsonResult.setData(result);
        return  jsonResult;
      }

//      查看抽奖次数
      @CrossOrigin
      @PostMapping("/showcount")
      public JsonResult<Integer> showCount(@RequestBody Map<String,Object> data){
          JsonResult<Integer> jsonResult=new JsonResult<>();
           Integer result=iLotteryPool.showCount((String) data.get("phone"), (String) data.get("name"));
           jsonResult.setState(SUCCESS);
           jsonResult.setData(result);
           return  jsonResult;
      }

    //      查看抽奖次数
    @CrossOrigin
    @PostMapping("/reducecount")
    public JsonResult<Void> reduceCount(@RequestBody Map<String,Object> data){
        JsonResult<Void> jsonResult=new JsonResult<>();
        iLotteryPool.reduceCount((String) data.get("phone"), (String) data.get("name"));
        jsonResult.setState(SUCCESS);
        return  jsonResult;
    }

//    实时显示最新中奖者，分页
    @CrossOrigin
    @PostMapping("/showhot")
    public JsonResult<List<ShowHotPrize>> showHotPrizeUser(@RequestBody Map<String,Object> data){
        JsonResult<List<ShowHotPrize>> jsonResult=new JsonResult<>();
        System.out.println("来到实时排行："+(String) data.get("name")+","+(Integer) data.get("currentPage")+","+(Integer) data.get("pageSize"));
        List<ShowHotPrize> showHotPrizes=iLotteryPool.showHotPrize((String) data.get("name"),(Integer) data.get("currentPage"),(Integer) data.get("pageSize"));
        jsonResult.setState(SUCCESS);
        jsonResult.setData(showHotPrizes);
        return  jsonResult;
    }




}
